﻿using System.Collections.Generic;
using System.Linq;
using xiushen.Infrastructure;
using xiushen.Models;

namespace xiushen.Domain
{
    
    public class  BookRepository:ReadOnlyRepository<Category>,IBookService
      {
        public Videos GetVideosByID(int categoryId, int id)
        {
            return QueryAll<Videos>().FirstOrDefault(r => r.CategoryID == categoryId && r.ID == id);
        }

        public IEnumerable<Book> SearchBooks(string name)
        {
            return
                QueryAll<Book>().Where(r => r.Name.Contains(name));
        }

        public IEnumerable<Videos> SearchVieos(string name)
        {
            return
                 QueryAll<Videos>().Where(r => r.Name.Contains(name));
        }

        public IEnumerable<Attachment> SearchAttachment(string name)
        {
            return QueryAll<Attachment>().Where(r =>r.FileType == Constants.Book &&  r.DisplayName.Contains(name) );
        }

        public IEnumerable<Category> GetAllCategorys()
        {
            return QueryAll().Where(r=> r.ParentID != null).ToList();
        }

          public IEnumerable<Category> GetRootCategorys()
          {
              return QueryAll().Where(r => r.ParentID == null && r.Type == Constants.Book);
          }

        public IEnumerable<Category> GetCategoriesByType(string type)
        {
            return QueryAll().Where(r => r.Type == type).ToList();
        }

        public IEnumerable<Category> GetSubCategorys(int parentId)
          {
              return QueryAll().Where(r => r.ParentID == parentId && r.Type == Constants.Book);

          }

          public Category GetByID(int categoryId)
          {
              return QueryAll<Category>().FirstOrDefault(r => r.ID == categoryId);
          }

          public IEnumerable<Attachment> GetPdfBooks(int categoryId)
          {
              return QueryAll<Attachment>().Where(r => r.CategoryID == categoryId);
          }

          public Book GetByID(int categoryId, int bookId)
          {
              return QueryAll<Book>().FirstOrDefault(r => r.ID == bookId && r.CategoryID == categoryId);
          }

          public IEnumerable<Book> GetBooksByCategory(int id)
          {
              return QueryAll<Book>().Where(r => r.CategoryID == id);
          }

          public IEnumerable<Chapter> GetChaptersByBookId(int id,int cateogryId)
          {
              return QueryAll<Chapter>().Where(r => r.BookID == id && r.CategoryID == cateogryId);
          }

          public Chapter GetChapterContent(int bookId, int chapterId,int categoryId)
          {
              return QueryAll<Chapter>().FirstOrDefault(r => r.BookID == bookId && r.ID == chapterId && r.CategoryID == categoryId);
          }

        public IEnumerable<Attachment> GetAttachments(int categoryId, string fileType)
        {
            return QueryAll<Attachment>().Where(r => r.CategoryID == categoryId && r.FileType == fileType).ToList();
        }

        public IEnumerable<Category> GetRecommandBooks()
        {
            return QueryAll<Category>().ToList();
        }
      }

    

        #region Nested type: IBookService

        public interface IBookService
        {
            Videos GetVideosByID(int categoryId,int id);
            IEnumerable<Book> SearchBooks(string name);
            IEnumerable<Videos> SearchVieos(string name);
            IEnumerable<Attachment> SearchAttachment(string name);
            IEnumerable<Category> GetAllCategorys();
            IEnumerable<Category> GetRootCategorys();
            IEnumerable<Category> GetCategoriesByType(string type);
            IEnumerable<Category> GetSubCategorys(int parentId);
            Category GetByID(int categoryId);
            IEnumerable<Attachment> GetPdfBooks(int categoryId);
            Book GetByID(int categoryId,int bookId);
            IEnumerable<Book> GetBooksByCategory(int id);
            IEnumerable<Chapter> GetChaptersByBookId(int bookId, int categoryId);
            Chapter GetChapterContent(int bookId, int chapterId, int categoryId);
            IEnumerable<Attachment> GetAttachments(int categoryId,string fileType);

            IEnumerable<Category> GetRecommandBooks();
        }

        #endregion
    }